package com.wuzhiming.istudy.controller;

import com.alibaba.fastjson.JSONObject;
import com.wuzhiming.istudy.pojo.Note;
import com.wuzhiming.istudy.utils.LogUtils;
import org.slf4j.Logger;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

/**
 * @ClassName: NoteController
 * @Description: 笔记控制器
 * @Author:zhimingw
 * @Version 1.0
 * @since 2022/3/16 20:01
 */
@RestController
public class NoteController extends BaseController{
    private Logger logger = LogUtils.getInstance(NoteController.class);

    @PostMapping("/addNote")
    public String addNote(Note note){
        note.setUserId(userInfo.getId());
        note.setTime(new Date());
        note.setStatus(1);
        if(note.getId()==null){
            if(noteService.addNote(note)){
                result.setCode(200);
            }else{
                result.setCode(500);
            }
        }else{
            if(noteService.update(note)){
                result.setCode(201);
            }else{
                result.setCode(501);
            }
        }
        return JSONObject.toJSONString(result);
    }

    /**
     * @Description: 修改笔记状态
     * @Param: [id]
     * @return: java.lang.String
     * @Author: zhimingw
     * @Date: 2021/7/20
     */
    @GetMapping("/changeNoteStatus")
    public String changeNoteStatus(@RequestParam("id") Integer id,@RequestParam("status") Integer status){
        Note note = new Note();
        note.setId(id);
        note.setStatus(status);
        if (noteService.update(note)) {
            result.setCode(200);
            logger.warn("【成功】修改笔记状态");
        }else {
            result.setCode(500);
            logger.warn("【失败】修改笔记状态");
        }
        return JSONObject.toJSONString(result);
    }
}
